// pages/LoginAndRegisterModule/register.js
import WxValidate from "../../utils/WxValidate";
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        auth_id: "",
        auth_name: "",
        auth_pwd: "",
        auth_repwd: "",
        auth_email: "",
        auth_email_identify: "",
        auth_gender: 1,
        auth_date: "",
        time: "",
        suffix: "获取验证码",
        current_time: 61,
        disabled:false,
        get_identify_color: "#34c9c3"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //校验数据
        this.initValidate();
    },

    register(e){
        let formData = this.data
        console.log(formData)
        if (!this.WxValidate.checkForm(formData)) {
            //表单元素验证不通过，此处给出相应提示
            let error = this.WxValidate.errorList[0];
            this.showModal(error);
            return false;
        };
        if(formData.auth_pwd != formData.auth_repwd){
            wx.showToast({
                title: '两次密码输入不一致',
                icon: "none",
                duration: 2000
            })
            return false;
        }
        wx.request({
            url: `http://${app.globalData.target}/permission/register`,
            data: {
              userId:this.data.auth_id,
              userName:this.data.auth_name,
              password:this.data.auth_pwd,
              birthday:this.data.auth_date,
              email:this.data.auth_email+'@hust.edu.cn',
              sex:this.data.auth_gender
            },
            method: 'POST',
            success: function(res){
              console.log(res)
              if (res.data.code != 0) {
                //注册失败
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000,
                  success: function(){
                    console.log('fail to register')
                    //刷新验证码操作
                  }
                })
              }else{
                //注册成功
                //存储用户信息
                
                //跳转页面
                wx.navigateTo({
                  url: '../market/market'
                })
              }
            },
            fail: function() {
              console.log('fail to link')
            },
        })
    },
    register_reset(e){
        this.setData({
            auth_id: "",
            auth_name: "",
            auth_pwd: "",
            auth_repwd: "",
            auth_email: "",
            auth_email_identify: "",
            auth_gender: "",
            auth_date: ""
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    bindIdChange: function(e){
        this.setData({
            auth_id: e.detail.value
        });
    },
    bindNameChange: function(e){
        this.setData({
            auth_name: e.detail.value
        });
    },
    bindPwdChange: function(e){
        this.setData({
            auth_pwd: e.detail.value
        });
    },
    bindRepwdChange: function(e){
        this.setData({
            auth_repwd: e.detail.value
        });
    },
    bindEmailChange: function(e){
        this.setData({
            auth_email: e.detail.value
        });
    },
    bindEmailIdentifyChange: function(e){
        this.setData({
            auth_email_identify: e.detail.value
        });
    },
    bindDateChange: function (e) {
        this.setData({
            auth_date: e.detail.value
        });
    },
    bindRadioChange: function(e){
        this.setData({
            auth_gender: Number(e.detail.value)
        })
    },
    getIdentifyCode: function(e){
        console.log(e)
        let _this = this
        if(_this.data.disabled == false){
            //第一次点
            let currentTime = this.data.current_time
            let interval = setInterval(function(){
                currentTime--
                _this.setData({
                    time: currentTime,
                    suffix: "秒后重新获取",
                    disabled: true,
                    get_identify_color: "gray"
                })
                if(currentTime <= 0){
                    clearInterval(interval)
                    _this.setData({
                        time: "",
                        suffix: "重新发送",
                        current_time: 61,
                        disabled: false,
                        get_identify_color: "#34c9c3"
                    })
                }
            }, 1000)
        }
    },

    /*校验数据 */
    initValidate() {
        let rules = {
            auth_id: {required: true,},
            auth_name: {required: true,},
            auth_pwd: {required: true,},
            auth_repwd: {required: true,},
            auth_email: {required: true,},
            auth_email_identify: {required: true,},
            auth_gender: {required: true,},
            auth_date: {required: true,}
        }
        //用于提示的信息
        let message = {
            auth_id: {required: '请输入用户名',},
            auth_name: {required: '请输入昵称',},
            auth_pwd: {required: '请输入密码',},
            auth_repwd: {required: '请重复输入密码',},
            auth_email: {required: '请输入邮箱',},
            auth_email_identify: {required: '请输入邮箱验证码',},
            auth_gender: {required: '请选择性别',},
            auth_date: {required: '请选择生日',},
        }
        //实例化当前的验证规则和提示消息
        this.WxValidate = new WxValidate(rules, message);
      },
      /**错误提示 */
      showModal(error) {
        wx.showToast({
          title: error.msg,
          icon: 'none',
          duration: 1000
        })
      },
})